<template>
  <div>
    <el-card>
      <h2>更改密码</h2>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="新用户名" prop="username">
          <el-input v-model.number="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input v-model="ruleForm.oldPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="ruleForm.newPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="m-t-20">
      <el-card>
        <h2>退出系统</h2>
        <el-button style="margin-top: 20px;" type="danger" @click="logout">退出系统</el-button>
      </el-card>
    </div>

  </div>
</template>

<script>
import userApi from "@/api/userApi";

export default {
  data() {
    return {
      ruleForm: {
        userId: "",
        username: '',
        oldPassword: '',
        newPassword: '',
      },
      rules: {
        oldPassword: [
          {required: true, message: '请输入旧密码', trigger: 'blur'},
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
        ]
      }
    };
  },
  created() {
  },
  methods: {
    checkPassword(password) {
      const reg = /^(?=.*[A-Za-z])(?=.*\d).+$/;
      return reg.test(password);
    },
    logout() {
      this.$confirm('将退出系统, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem("userInfo")
        window.location.reload();
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.checkPassword(this.ruleForm.newPassword)) {
            const userInfo = JSON.parse(localStorage.getItem("userInfo"))
            this.ruleForm.userId = userInfo.username
            userApi.updatePassword(this.ruleForm).then(resp => {
              if (resp.code === 200) {
                this.$message.success(resp.message)
                setTimeout(() => {
                  localStorage.removeItem("userInfo")
                  window.location.reload();
                }, 2000)
              } else {
                this.$message.error(resp.message)
              }
            })
          } else {
            this.$message.error("密码必须包含字母和数字！")
          }

        } else {
          this.$message.error("表单存在问题，请查看");
          return false;
        }
      });
    },
  }
}

</script>
<style scoped>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  /* 两个卡片之间的间距 */
  padding: 20px;
}

.el-card {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  /* 添加阴影 */
  width: 100%;
  /* 或者其他合适的宽度 */
  max-width: 500px;
  /* 最大宽度 */
}
</style>
